package feeinterface.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ulwx.tool.CTime;
import com.ulwx.tool.DbUtils;

import feeinterface.modeldb.BaseArea;
import feeinterface.protocol.utils.Constants;


public class BaseAreaDao {

	public static BaseArea getByAreaCode(String areaCode) throws Exception{
		String sql = "select * from BaseArea where "
				+ "AreaCode=?";
		
		//System.out.println(CTime.getCurrentDate());
		Map<Integer, Object> map = new HashMap<Integer, Object>();
		if(areaCode.substring(0,2).equals("01") || areaCode.substring(0,2).equals("02") )
		{
			areaCode = areaCode.substring(0, 3);
		}
		map.put(1, areaCode);
		List<BaseArea> list = DbUtils.doQueryClass(null,
				BaseArea.class, sql, map);
		if (list.size() >= 1)
			return list.get(0);
		return null;
	}

	/**
	 * 判断是否为北京地区的用户
	 * 2010-12-22
	 * @param areaID
	 * @return
	 * @throws Exception
	 */
	public static boolean isBeijing(Integer areaID) throws Exception{
		boolean isBeijing = false;
		String sql = "select * from BaseArea where "
				+ " AreaName = '北京' AND AreaSno = ? ";
	
		Map<Integer, Object> map = new HashMap<Integer, Object>();
		map.put(1, areaID);
		List<BaseArea> list = DbUtils.doQueryClass(null,
				BaseArea.class, sql, map);
		if (list != null && list.size() >= 1)
		{
			isBeijing = true;
		}
		return isBeijing;
	}
	
	//返回该区父级ID
	public static int getParentIDByAreaID(Integer areaID) throws Exception{
		String sql = "select * from BaseArea where AreaSno = ? ";
	
		Map<Integer, Object> map = new HashMap<Integer, Object>();
		map.put(1, areaID);
		List<BaseArea> list = DbUtils.doQueryClass(null,BaseArea.class, sql, map);
		if (list != null && list.size() >= 1){
			BaseArea baseArea = list.get(0);
			return baseArea.getParentID();
		}
		return 0;
	}
	
	/*
	 * 根据省份ID或城市id取得省份ID和城市ID
	 * 2010-1-26
	 * 
	 */
	public static int[] getProvinceAndCityID(Integer areaID) throws Exception
	{
		int[] areaIDs = {Constants.Area.UNKNOWN_PROVINCEID ,Constants.Area.UNKNOWN_CITYID};
		String sql = "select * from BaseArea where AreaSno = ? ";
		//DataBase db = DataBaseFactory.getDataBase();
		Map<Integer, Object> map = new HashMap<Integer, Object>();
		map.put(1, areaID);
		List<BaseArea> list = DbUtils.doQueryClass(null,BaseArea.class, sql, map);
		if (list != null && list.size() >= 1)
		{
			BaseArea baseArea = list.get(0);
			if(baseArea.getAreaFlag() == 3)
			{
				//传入的是城市ID，取得省份id
				areaIDs[0] = baseArea.getParentID();
				areaIDs[1] = areaID;
			}
			else if(baseArea.getAreaFlag() == 2)
			{
				//传入的是省份ID,则城市设置为未知
				sql = "SELECT * FROM BaseArea WHERE parentID = ? AND AreaName = '未知'";
				//db.reConnectDb();
				list = DbUtils.doQueryClass(null,BaseArea.class, sql, map);
				areaIDs[0] = areaID; 
				if (list != null && list.size() >= 1)
				{
					baseArea = list.get(0);
					areaIDs[1] =  baseArea.getAreaSno();
				}else{
					areaIDs[1] =0;
				}
			}
			else
			{
//				sql = "SELECT * FROM BaseArea WHERE AreaFlag = 3 AND AreaName = '未知'";
//				db.reConnectDb();
//				list = db.doQueryClass(BaseArea.class, sql, map);
//				if (list != null && list.size() >= 1)
//				{
//					areaIDs[0] = areaID; 
//					areaIDs[1] =  baseArea.getAreaSno();
//				}
			}
		}
		return areaIDs;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		System.out.println(CTime.getCurrentDate());
		BaseArea ba = getByAreaCode("0200");
		System.out.println(ba.getAreaCode());
		System.out.println(CTime.getCurrentDate());
		ba = getByAreaCode("0210");
		System.out.println(ba.getAreaCode());
		System.out.println(CTime.getCurrentDate());
		ba = getByAreaCode("0220");
		System.out.println(ba.getAreaCode());
		System.out.println(CTime.getCurrentDate());
	}

}
